<?php
class Advertise_Models_Dao_Pdo_Mysql_Advertise extends Tomato_Model_Dao
    implements Advertise_Models_Interface_Advertise
{
    public function convert($entity)
    {
        return new Advertise_Models_Advertise($entity);
    }

    public function update($advertise){
        $sql = "INSERT INTO shop_product_market_data (product_id, category_id, advertise) VALUES ({$advertise['product_id']},{$advertise['category_id']},{$advertise['advertise']}) ON DUPLICATE KEY UPDATE category_id={$advertise['category_id']}, advertise={$advertise['advertise']}";
        return $this->_conn->query($sql);
    }

    public function advertise($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('md' => $this->_prefix.'shop_product_market_data'))
                        ->where("md.category_id = ?",$id)
                        ->where("md.advertise = ?",1)
                        ;
        $rowset = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rowset, $this);
    }

    public function unadvertise($cat_id, $strid){
        $where[] = "category_id = {$cat_id} AND ',{$strid},' NOT LIKE concat('%,',product_id,',%')";
        $this->_conn->update($this->_prefix . 'shop_product_market_data',
                            array(
                                'advertise' => 0,
                            ),$where);
    }

    /***================enterprise=================*/

    public function enUpdate($advertise){
        $sql = "INSERT INTO enterprise_market_data (enterprise_id, advertise) VALUES ({$advertise['enterprise_id']},{$advertise['advertise']}) ON DUPLICATE KEY UPDATE advertise={$advertise['advertise']}";
        return $this->_conn->query($sql);
    }

    public function enAdvertise(){
        $select = $this->_conn
                        ->select()
                        ->from(array('md' => $this->_prefix.'enterprise_market_data'))
                        ->where("md.advertise = ?",1)
                        ;
        $rowset = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rowset, $this);
    }

    public function enUnadvertise($strid){
        $where[] = "',{$strid},' NOT LIKE concat('%,',enterprise_id,',%')";
        $this->_conn->update($this->_prefix . 'enterprise_market_data',
                            array(
                                'advertise' => 0,
                            ),$where);
    }
}
